package edu.dayu.service.impl;

import edu.dayu.base.BaseJob;
import edu.dayu.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class JobServiceImpl implements JobService {
    @Autowired
    @Qualifier(value = "Scheduler")
    private Scheduler scheduler;

    public static BaseJob getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (BaseJob)class1.newInstance();
    }

    @Override
    public boolean addJob(String jobClassName, String jobGroupName, String cronExpress) {
        try {
            // 启动调度器
            scheduler.start();
            //构建job信息
            JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobClassName, jobGroupName).build();

            //表达式调度构建器(即任务执行的时间)
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpress);

            //按新的cronExpression表达式构建一个新的trigger
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
                    .withSchedule(scheduleBuilder).build();

            scheduler.scheduleJob(jobDetail, trigger);
            log.info("任务"+jobClassName+"创建成功");
            return true;
        } catch (Exception e) {
            log.error("任务"+jobClassName+"创建失败");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean pauseJob(String jobClassName, String jobGroupName) {
        try {
            scheduler.pauseJob(JobKey.jobKey(jobClassName,jobGroupName));
            log.info("任务"+jobClassName+"暂停成功");
            return true;
        } catch (SchedulerException e) {
            log.error("任务"+jobClassName+"暂停失败");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean resumeJob(String jobClassName, String jobGroupName) {
        try {
            scheduler.resumeJob(JobKey.jobKey(jobClassName,jobGroupName));
            log.info("任务"+jobClassName+"继续任务");
            return true;
        } catch (SchedulerException e) {
            log.error("任务"+jobClassName+"继续任务失败");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean rescheduleJob(String jobClassName, String jobGroupName, String cronExpression) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            // 按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();

            // 按新的trigger重新设置job执行
            scheduler.rescheduleJob(triggerKey, trigger);
            log.info("任务"+jobClassName+"任务更新成功");
            return true;
        } catch (Exception e) {
            log.error("任务"+jobClassName+"任务更新失败");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteJob(String jobClassName, String jobGroupName) {
        try {
            scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
            scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
            scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
            log.info("任务"+jobClassName+"任务删除成功");
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }
}
